package com.googlecode.grima.bpm;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.googlecode.grima.domain.Descriptive;
import com.googlecode.grima.domain.DescriptiveItem;
import com.googlecode.grima.service.DescriptiveService;

public class CreateDescriptive implements JavaDelegate {

	private DescriptiveService descriptiveService;
	
	@Override
  public void execute(DelegateExecution execution) throws Exception {

		ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("classpath:persistence-context.xml");
		descriptiveService = (DescriptiveService) cpx.getBean("descriptiveService");
		
		String processInstanceId = execution.getProcessInstanceId();
		
		Descriptive descriptive = descriptiveService.getNew();
		descriptive.setProcessInstanceId(processInstanceId);
		
		for (int a = 1; a <= 100; a++){
			descriptive.getItems().add(new DescriptiveItem());
		}
		
		descriptiveService.save(descriptive);
		
		Long id = descriptive.getId();
		execution.setVariable("descriptive.id", id);

		cpx.close();
		
  }

}
